public class Solution {
    class ListNode {
     int val;
    ListNode next;
    ListNode(int x) {
        val = x;
        next = null;
    }
 }
        public ListNode detectCycle(ListNode head) {
            ListNode fast = head;
            ListNode slow = head;
            while(fast!=null&&fast.next!=null){
                fast = fast.next.next;
                slow = slow.next;
                if(fast == slow){
                    break;
            }
        }

        //两种情况，1.循环条件不满足2.break
       if(fast == null||fast.next==null){
        return null;
       }
                //初始点到入口点的距离等于在环里相遇点到入口点的距离
                    slow = head;
                    //让slow回到入口点当他俩再次相遇时就是入口点
                    while(fast != slow){
                        fast = fast.next;
                        slow = slow.next;
                    }
                    return fast;
                }
}
